<?php
/**
 * XIdea A PHP Framework For Simple
 * @author xyb <781028081@qq.com>
 * @version 1.0
 */

/**
 *  这个文件主要是用来创建App模块的
 * @author xyb <781028081@qq.com>
 * @version 1.0
 */

define('DS', DIRECTORY_SEPARATOR);
define('APPLICATION', __DIR__ . DS . 'app');
define('TEMPLATE', __DIR__ . DS . 'xidea' . DS . 'templates' . DS . 'apper');

class Apper
{
    private $appname;
    private $app;
    private $controllers;
    private $models;
    private $events;
    private $config;
    private $views;
    private $helpers;
    private $index;

    public function __construct($argv)
    {
        $this->appname = $argv[1];
        $this->app = APPLICATION . DS . $this->appname;
        $this->controllers = $this->app . DS . 'controllers';
        $this->models = $this->app . DS . 'models';
        $this->events = $this->app . DS . 'events';
        $this->config = $this->app . DS . 'config';
        $this->views = $this->app . DS . 'vies';
        $this->helpers = $this->app . DS . 'helpers';
        $this->index = $this->app . DS . 'index.php';
    }

    public function run()
    {
        !is_dir($this->app) ? mkdir($this->app) : null;
        !is_dir($this->controllers) ? $this->_controller() : null;
        !is_dir($this->models) ? mkdir($this->models) : null;
        !is_dir($this->events) ? mkdir($this->events) : null;
        !is_dir($this->config) ? $this->_config() : null;
        !is_dir($this->views) ? mkdir($this->views) : null;
        !is_dir($this->helpers) ? mkdir($this->helpers) : null;
        !is_file($this->index) ? $this->_index() : null;
    }

    private function _controller()
    {
        mkdir($this->controllers);

        $tpl = TEMPLATE . '/SiteController.tpl';
        $content = str_replace('%app%', $this->appname, file_get_contents($tpl));

        file_put_contents($this->controllers . DS . 'SiteController.php', $content);
    }

    private function _index()
    {
        $tpl = TEMPLATE . '/index.tpl';
        file_put_contents($this->index, file_get_contents($tpl));
    }

    private function _config()
    {
        mkdir($this->config);

        $tpl = TEMPLATE . '/config.tpl';
        file_put_contents($this->config . DS . 'config.php', file_get_contents($tpl));
        file_put_contents($this->config . DS . 'params.php', file_get_contents($tpl));
    }
}

$apper = new Apper($argv);
$apper->run();

